package emulator.shell.GUI.util;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class DialogBase extends JDialog
{
	private boolean result = false;

	public DialogBase() {
		super();
	}

	public DialogBase(Frame owner) {
		super(owner);
	}

	public DialogBase(Dialog owner) {
		super(owner);
	}

	public DialogBase(Window owner) {
		super(owner);
	}

	public DialogBase(Frame owner, boolean modal) {
		super(owner, modal);
	}

	public DialogBase(Frame owner, String title) {
		super(owner, title);
	}

	public DialogBase(Dialog owner, boolean modal) {
		super(owner, modal);
	}

	public DialogBase(Dialog owner, String title) {
		super(owner, title);
	}

	public DialogBase(Window owner, ModalityType modalityType) {
		super(owner, modalityType);
	}

	public DialogBase(Window owner, String title) {
		super(owner, title);
	}

	public DialogBase(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
	}

	public DialogBase(Dialog owner, String title, boolean modal) {
		super(owner, title, modal);
	}

	public DialogBase(Window owner, String title, ModalityType modalityType) {
		super(owner, title, modalityType);
	}

	public DialogBase(Frame owner, String title, boolean modal,
			GraphicsConfiguration gc) {
		super(owner, title, modal, gc);
	}

	public DialogBase(Dialog owner, String title, boolean modal,
			GraphicsConfiguration gc) {
		super(owner, title, modal, gc);
	}

	public DialogBase(Window owner, String title, ModalityType modalityType,
			GraphicsConfiguration gc) {
		super(owner, title, modalityType, gc);
	}

	public void setMargins(JComponent component, int top, int left, int bottom, int right)
	{
		Border current = component.getBorder();
		Border empty = new EmptyBorder(top, left, bottom, right);
	    component.setBorder(current == null ? empty : new CompoundBorder(empty, current));
	}

	public void setMargins(JComponent component, int dy, int dx)
	{
		setMargins(component, dy, dx, dy, dx);
	}
	
	public void setMargins(JComponent component, int margin)
	{
		setMargins(component, margin, margin, margin, margin);
	}
	
	public boolean getResult() {
		return result;
	}

	protected void closeDialog(boolean result) {
		this.result = result;
		this.setVisible(false);
	}

	public JPanel createButtonPanel()
	{
		JButton buttonOk = new JButton("Ok");
		buttonOk.addActionListener(new OnOk());
		JButton buttonCancel = new JButton("Cancel");
		buttonCancel.addActionListener(new OnCancel());

		JPanel buttonPanel = new JPanel();
		GroupLayout layout = new GroupLayout(buttonPanel);
		buttonPanel.setLayout(layout);
		
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		Component box1 = Box.createHorizontalGlue();
		Component box2 = Box.createHorizontalGlue();
		
		layout.setHorizontalGroup(
				   layout.createSequentialGroup()
				   	  .addComponent(box1)
				      .addComponent(buttonOk)
				      .addComponent(buttonCancel)
				   	  .addComponent(box2)
				      );
		layout.setVerticalGroup(
				   layout.createParallelGroup(GroupLayout.Alignment.CENTER)
				   	  .addComponent(box1)
				      .addComponent(buttonOk)
				      .addComponent(buttonCancel)
				   	  .addComponent(box2)
				      );
		layout.linkSize(buttonOk, buttonCancel);
		
		return buttonPanel;
	}
	
	public class OnCancel implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			closeDialog(false);
		}
	}

	public class OnOk implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e)
		{
			closeDialog(true);
		}
	}
}